<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商名称" label-width="100" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商代码" label-width="100" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" label-width="100" prop="supplierPhone">
        <el-input
          v-model="queryParams.supplierPhone"
          placeholder="请输入供应商电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplychain:supplier:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplychain:supplier:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplychain:supplier:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplychain:supplier:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公司代码" align="center" width="80" prop="factoryCode"/>
      <el-table-column label="公司名称" align="center" width="80" prop="factoryName"/>
<!--      <el-table-column label="供应商类别" align="center" prop="supplierCategory" width="120"/>-->
      <el-table-column label="供应商代码" align="center" prop="supplierCode" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
          >{{ scope.row.supplierCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName" width="200"/>
      <el-table-column label="供应商税号" align="center" prop="supplierEin" width="200"/>
      <el-table-column label="供应商地址" align="center" prop="address" show-overflow-tooltip width="200"/>
      <el-table-column label="供应商电话" align="center" prop="supplierPhone" width="120"/>
      <el-table-column label="供应商开户行" align="center" prop="supplierBank" width="200" show-overflow-tooltip/>
      <el-table-column label="供应商账号" align="center" prop="supplierAcc" width="200"/>
      <el-table-column label="供应商联系人" align="center" prop="supplierContact" width="120"/>
      <el-table-column label="供应商联系人电话" align="center" prop="supplierContactNumber" width="140"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplychain:supplier:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplychain:supplier:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商管理对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司代码" prop="factoryCode">
              <el-input disabled v-model="form.factoryCode" placeholder="请输入公司代码">
<!--                <el-button slot="append" icon="el-icon-search" @click="handleSelectOrganizational"></el-button>-->
              </el-input>
            </el-form-item>
            <OrganizationalSelect ref="organizationalSelect" @onSelected="onOrganizationalSelected" ></OrganizationalSelect>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="factoryName">
              <el-input disabled v-model="form.factoryName" placeholder="请输入公司名称"/>
            </el-form-item>
          </el-col>

        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="供应商类别" prop="supplierCategory">-->
<!--              <el-select v-model="form.supplierCategory" placeholder="请选择">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.supplier_category"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="供应商代码" prop="supplierCode">-->
<!--              <el-input v-model="form.supplierCode" placeholder="请输入供应商代码"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商税号" prop="supplierEin">
              <el-input v-model="form.supplierEin" placeholder="请输入供应商税号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商电话" prop="supplierPhone">
              <el-input v-model="form.supplierPhone" placeholder="请输入供应商电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商开户行" prop="supplierBank">
              <el-input v-model="form.supplierBank" placeholder="请输入供应商开户行"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商账号" prop="supplierAcc">
              <el-input v-model="form.supplierAcc" placeholder="请输入供应商账号"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商联系人" prop="supplierContact">
              <el-input v-model="form.supplierContact" placeholder="请输入供应商联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商联系人电话" prop="supplierContactNumber">
              <el-input v-model="form.supplierContactNumber" placeholder="请输入供应商联系人电话"/>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="运输方式" prop="tsWays">-->
<!--              <el-select v-model="form.tsWays" multiple placeholder="请选择">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.transportatio_mode"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="运输类别" prop="tsType">-->
<!--              <el-select v-model="form.tsType" placeholder="请选择">-->
<!--                <el-option-->
<!--                  v-for="dict in dict.type.transportation_category"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.label"-->
<!--                  :value="dict.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="供应商运输距离(km)" prop="supplierTsKm">-->
<!--              <el-input v-model="form.supplierTsKm" placeholder="请输入供应商运输距离"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="供应商运输时间(小时)" prop="supplierTsTime">-->
<!--              <el-input v-model="form.supplierTsTime" placeholder="请输入供应商运输时间"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier} from "@/api/supplychain/supplier";
import {getListClass} from "@/api/base/class";
import {getToken} from "@/utils/auth";
import OrganizationalSelect  from "@/components/organizationalSelect/index";

export default {
  name: "Supplier",
  dicts: ['supplier_category','transportatio_mode','transportation_category'],
  components :{OrganizationalSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      classList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        materialCategory: null,
        itemChildNo: null,
        materialNo: null,
        supplierCategory: null,
        materialName: null,
        supplierName: null,
        supplierCode: null,
        address: null,
        supplierPhone: null,
        supplierEin: null,
        supplierBank: null,
        supplierAcc: null,
        proCycle: null,
        supplierTsKm: null,
        supplierTsTime: null,
        deliveryCycle: null,
        deliveryFe: null,
        adjustRate: null,
        adjustDate: null,
        orderNumMonth: null,
        finishNumMonth: null,
        nofinishRateMonth: null,
        packageCapacity: null,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入`NcSupplier`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/supplychain/supplier/importData"
      },
      // 表单参数
      form: {
        factoryCode:"ZQEY",
        factoryName:'济南市章丘区第二人民医院'
      },
      // 表单校验
      rules: {
        factoryCode: [
          { required: true, message: "工程代码不能为空", trigger: "blur" }
        ],
        supplierCode: [
          { required: true, message: "供应商代码不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: "ZQEY",
        factoryName: "济南市章丘区第二人民医院",
        materialCategory: null,
        itemChildNo: null,
        materialNo: null,
        supplierCategory: null,
        materialName: null,
        supplierName: null,
        supplierCode: null,
        address: null,
        supplierPhone: null,
        supplierEin: null,
        supplierBank: null,
        supplierAcc: null,
        proCycle: null,
        supplierTsKm: null,
        supplierTsTime: null,
        deliveryCycle: null,
        deliveryFe: null,
        adjustRate: null,
        adjustDate: null,
        orderNumMonth: null,
        finishNumMonth: null,
        nofinishRateMonth: null,
        packageCapacity: null,
        supplierContact: null,
        supplierContactNumber: null,
        tsWays: null,
        tsType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getListClass().then(resp => {
        this.classList = resp.data;
        this.reset();
        this.open = true;
        this.title = "添加供应商管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商管理编号为"' + ids + '"的数据项？').then(function () {
        return delSupplier(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supplychain/supplier/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "供应商档案/份额调整/份额控制总导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/supplychain/supplier/importTemplate', {
      }, `供应商档案.xlsx`)
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleSelectOrganizational(){
      this.$refs.organizationalSelect.showFlag = true;
    },
    onOrganizationalSelected(obj){
      if(obj != undefined && obj != null){
        this.form.factoryCode = obj.jtdm;
        this.form.factoryName = obj.jtmc;
      }
    },
  }
};
</script>
